package cn.hznc.controller.program;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.aspect.BusinessType;
import cn.hznc.aspect.Log;
import cn.hznc.controller.program.strategy.AbutmentSubprogramContext;
import cn.hznc.controller.program.strategy.ZccAbutmentSubprogramStrategy;
import cn.hznc.domain.entity.EquipmentShankModelEntity;
import cn.hznc.domain.request.program.*;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.controller.service.program.SubProgramManageService;
import cn.hznc.dto.SubProgramImport;
import cn.hznc.excel.ProgramReadListener;
import cn.hznc.exception.SystemException;
import cn.hznc.permission.Permission;
import cn.hznc.utils.FileUploadUtil;
import cn.hznc.vo.ProgramVo;
import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * 子程序管理接口
 *
 * @Author Administrator
 * @Date 2023/3/13 16:00
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/subProgram")
@Slf4j
public class SubProgramManageController {
    @Autowired
    private SubProgramManageService subProgramManageService;

    @Resource
    private ZccAbutmentSubprogramStrategy zccAbutmentSubprogramStrategy;


    /**
     * 子程序目录查询
     *
     * @param querySubProgramReq querySubProgramReq
     * @return
     */
    @RequestMapping(value = "/querySubProgramList", method = RequestMethod.POST)
    public ResponseBody querySubProgramList(@RequestBody QuerySubProgramReq querySubProgramReq) {
        return ResponseBody.success(subProgramManageService.querySubProgramList(querySubProgramReq));
    }

    /**
     * 导出子程序列表
     *
     * @return
     */
    @RequestMapping(value = "/exportSubprogramList", method = RequestMethod.POST)
    public void exportSubprogramList(HttpServletResponse response , @RequestBody ExportSubprogramListReq exportSubprogramListReq) {
        subProgramManageService.exportSubprogramList(response , exportSubprogramListReq);
    }

    /**
     * 子程序目录批量删除修改，新增
     *
     * @param addOrEditSubProgramReq addOrEditSubProgramReq
     * @return
     */
    @RequestMapping(value = "/addOrEditSubProgram", method = RequestMethod.POST)
    @Log(title = "新增或编辑子程序" , businessType = BusinessType.OTHER)
    public ResponseBody<ProgramVo> addOrEditSubProgram(@RequestBody AddOrEditSubProgramReq addOrEditSubProgramReq) {
        if(ObjectUtil.isNotNull(addOrEditSubProgramReq.getFlag()) && addOrEditSubProgramReq.getFlag()){
            //保存新版本
            if(CollectionUtil.isNotEmpty(addOrEditSubProgramReq.getVersionList())){
                addOrEditSubProgramReq.setProgramVersion(Collections.max(addOrEditSubProgramReq.getVersionList()) + 1);
            }
            return ResponseBody.success(subProgramManageService.addOrEditSubProgramReload(addOrEditSubProgramReq));
        }
        //修改旧版本
        return ResponseBody.success(subProgramManageService.addOrEditSubProgram(addOrEditSubProgramReq));
    }

    /**
     * 子程序目录批量删除修改，新增
     *
     * @return
     */
    @RequestMapping(value = "/addOrEditSubProgramReload", method = RequestMethod.POST)
    @Log(title = "新增或编辑子程序" , businessType = BusinessType.OTHER)
    public ResponseBody addOrEditSubProgramReload(@RequestBody AddOrEditSubProgramReloadReq addOrEditSubProgramReloadReq) {
        if(ObjectUtil.isNotNull(addOrEditSubProgramReloadReq.getFlag()) && addOrEditSubProgramReloadReq.getFlag()){
            //保存新版本
            if(CollectionUtil.isNotEmpty(addOrEditSubProgramReloadReq.getVersionList())){
                //addOrEditSubProgramReloadReq.setProgramVersion(Collections.max(addOrEditSubProgramReloadReq.getVersionList()) + 1);
                addOrEditSubProgramReloadReq.setNewProgramVersion(Collections.max(addOrEditSubProgramReloadReq.getVersionList()) + 1);
            }
            return ResponseBody.success(subProgramManageService.addOrEditSubProgramReload1(addOrEditSubProgramReloadReq));
        }
        //修改旧版本
        return ResponseBody.success(subProgramManageService.addOrEditSubProgram1(addOrEditSubProgramReloadReq));
    }

    /**
     * 查询子程序版本号列表
     *
     * @return
     */
    @RequestMapping(value = "/querySubProgramVersion", method = RequestMethod.POST)
    public ResponseBody querySubProgramVersion(@RequestBody QuerySubprogramVersionReq querySubprogramVersionReq) {
        return ResponseBody.success(subProgramManageService.querySubProgramVersion(querySubprogramVersionReq));
    }

    /**
     * 子程序目录批量删除修改，新增
     *
     * @return
     */
    @RequestMapping(value = "/saveAsNewProgram", method = RequestMethod.POST)
    public ResponseBody saveAsNewProgram(@RequestBody SaveAsProgramReq saveAsProgramReq) {
        return ResponseBody.success(subProgramManageService.saveAsNewProgram(saveAsProgramReq));
    }

    /**
     * 程序看板导入
     *
     * @param file
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/importProgramBroad", method = RequestMethod.POST)
    public ResponseBody<Boolean> importProgramBroad(@RequestParam("file") MultipartFile file, HttpServletRequest httpServletRequest) {
        try {
            ProgramReadListener programReadListener = new ProgramReadListener();
            FileUploadUtil.checkCommonFile(file);
            EasyExcel.read(file.getInputStream(), SubProgramImport.class, programReadListener).headRowNumber(2).autoCloseStream(true).sheet().doRead();
            subProgramManageService.importProgramBroad(programReadListener.getImportData());
        } catch (IOException ex) {
            log.error("importOrder error========》", ex);
        } catch (SystemException systemException) {
            log.error("importOrder error========》msg：{}", systemException.getMsg());
            return ResponseBody.error(systemException.getCode(), systemException.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }

    /**
     * 查看主程序对应的程序看板
     *
     * @return
     */
    @RequestMapping(value = "/checkSubProgramGroup", method = RequestMethod.GET)
    public ResponseBody checkSubProgramGroup() {
        return ResponseBody.success(subProgramManageService.checkSubProgramGroup());
    }

    /**
     * 查看主程序对应的程序看板
     *
     * @return
     */
    @RequestMapping(value = "/abutmentSubProgram", method = RequestMethod.GET)
    public ResponseBody abutmentSubProgram() {
        return ResponseBody.success(subProgramManageService.abutmentSubProgram());
    }


//    /**
//     * 查看主程序对应的程序看板
//     *
//     * @return
//     */
//    @RequestMapping(value = "/checkSubProgram", method = RequestMethod.POST)
//    public ResponseBody checkSubProgram(@RequestParam("programCode") String programCode , @RequestParam("groupType") String groupType) {
//        return ResponseBody.success(subProgramManageService.checkSubProgram(programCode , groupType));
//    }

    /**
     * 查看主程序对应的程序看板
     *
     * @return
     */
    @RequestMapping(value = "/editEquipmentShankModel", method = RequestMethod.POST)
    @Log(title = "编辑程序后处理刀柄信息" , businessType = BusinessType.UPDATE)
    public ResponseBody editEquipmentShankModel(@RequestBody EquipmentShankModelEntity equipmentShankModelEntity) {
        return ResponseBody.success(subProgramManageService.editEquipmentShankModel(equipmentShankModelEntity));
    }

    /**
     * 查看主程序对应的程序看板
     *
     * @return
     */
    @RequestMapping(value = "/qryShankModelByEquipmentGroup", method = RequestMethod.GET)
    public ResponseBody qryShankModelByEquipmentGroup(@RequestParam Integer equipmentId) {
        return ResponseBody.success(subProgramManageService.qryShankModelByEquipmentGroup(equipmentId));
    }

    /**
     * 查看主程序对应的程序看板
     *
     * @return
     */
    @RequestMapping(value = "/abutmentSubprogram", method = RequestMethod.GET)
    public ResponseBody abutmentSubprogram(@RequestParam("programNums") List<String> programNums) {
        AbutmentSubprogramContext abutmentSubprogramContext = new AbutmentSubprogramContext(zccAbutmentSubprogramStrategy);
        abutmentSubprogramContext.abutmentSubprogram(programNums);
        return ResponseBody.success();
        //return ResponseBody.success(subProgramManageService.abutmentSubProgramReload(programNums));
    }

    /**
     * 查看主程序对应的程序看板
     *
     * @return
     */
    @RequestMapping(value = "/abutmentSubprogramReload", method = RequestMethod.GET)
    public ResponseBody abutmentSubprogramReload() {
        return ResponseBody.success(subProgramManageService.abutmentSubProgramTest());
    }

    /**
     * 查看主程序对应的程序看板
     *
     * @return
     */
    @RequestMapping(value = "/qryEquipmentIds", method = RequestMethod.GET)
    public ResponseBody qryEquipmentIds() {
        return ResponseBody.success(subProgramManageService.qryEquipmentIds());

    }

    /**
     * 查看主程序对应的程序看板
     *
     * @return
     */
    @RequestMapping(value = "/addSubProgramNameDuration", method = RequestMethod.GET)
    public ResponseBody addSubProgramNameDuration() {
        return ResponseBody.success(subProgramManageService.addSubProgramNameDuration());
    }

    /**
     * 查看主程序对应的程序看板
     *
     * @return
     */
    @RequestMapping(value = "/editSubPrograms", method = RequestMethod.POST)
    @Permission(title = "编辑子程序" , permissions = {"subprogram.edit"})
    public ResponseBody editSubPrograms(@RequestBody EditSubprogramReq editSubprogramReq) {
        return ResponseBody.success(subProgramManageService.editSubPrograms(editSubprogramReq));
    }

}
